$(function() {
	$("#resend").click(function(){
		$(this).attr("disabled", true);  
		var email = $("#email input").val();
		var resultModal = $("#result-modal");
		var modalTitle = resultModal.find(".modal-body p:first");
		modalTitle.html("<span class=\"text-primary\">正在为您重新发送邮件，请稍等...</span>");
		$.ajax({
			url: "resen-Verification",
			type: "POST",
			dataType: "text json",
			success:function(data){
				resultModal.modal("show");
				if (data.state == "success") {
					modalTitle.html("<span class=\"text-success\">已重新发送一份新的验证邮件，请登陆您的邮箱" + email + "，点击邮件中的链接完成注册！</span>");
					$("#resend").removeAttr("disabled");
				}
				else if (data.state == "error")
				{
					modalTitle.html("<span class=\"text-danger\">抱歉,发生了一些错误，无法重新发送验证邮件，请稍后重试！</span>");
					$("#resend").removeAttr("disabled");
				}
				else if (data.state == "hasVerification")
				{
					$("#result-modal").modal("show");
					modalTitle.html("<span class=\"text-success\">您的邮箱已验证，无需重复验证！</span>");
				}
			},
			error:function(XMLHttpRequest, textStatus, errorThrown) {
				alert("XMLHttpRequest.status : " + XMLHttpRequest.status +
					"\nXMLHttpRequest.readyState : " + XMLHttpRequest.readyState +
					"\ntextStatus : " + textStatus
				)
			}
		});
	});
	$("#login-form").submit(function(){
		var email = $("#email input").val();
		$.ajax({
			url: "login",
			type: "POST",
			dataType: "text json",
			data: {
				"user.email" : email,
				"user.passwd" : $("#passwd input").val()
			},
			success:function(data){
				if (data.state == "success") {
					$("#err-alert").hide();
					location.href = "index";
				}
				else if (data.state == "error")
				{
					showAlertError("账号或密码错误");
				}
				else if (data.state == "noVerification")
				{
					var resultModal = $("#result-modal");
					resultModal.modal("show");
					resultModal.find(".modal-body p:first").html("请登陆您的邮箱<span>" + email + "</span>，点击邮件中的链接完成注册！");
				}
			},
			error:function(XMLHttpRequest, textStatus, errorThrown) {
				alert("XMLHttpRequest.status : " + XMLHttpRequest.status +
					"\nXMLHttpRequest.readyState : " + XMLHttpRequest.readyState +
					"\ntextStatus : " + textStatus
				)
			}
		});
		return false;
	});
	$("#signup-form").submit(function() {
		$(this).find("button").attr("disabled", true); 
		var email = $("#email input").val();
		$.ajax({
			url: "signup",
			type: "POST",
			dataType: "text json",
			data: {
				"user.name" : $("#usrname input").val(),
				"user.email" : email,
				"user.passwd" : $("#passwd input").val()
			},
			success: function(data) {
				if (data.state == "success") {
					$("#err-alert").hide();
					$("#email").removeClass("has-error");
					$("#name").removeClass("has-error");
					var resultModal = $("#result-modal");
					resultModal.modal("show");
					resultModal.find(".modal-body span").text(email);
				}
				else if (data.state == "error")
				{
					if (data.error == "email")
					{
						$("#email").addClass("has-error");
						showAlertError("邮箱已存在");
					}
					else if (data.error == "name")
					{
						$("#usrname").addClass("has-error");
						showAlertError("用户名已存在");
					}
					else if (data.error == "unknow")
					{
						showAlertError("发生未知错误");
					}
				}
				$("#signup-form button").removeAttr("disabled");
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				alert("XMLHttpRequest.status : " + XMLHttpRequest.status +
					"\nXMLHttpRequest.readyState : " + XMLHttpRequest.readyState +
					"\ntextStatus : " + textStatus
				);
			}
		});
		return false;
	});
	function showAlertError(msg)
	{
		var errAlert = $("#err-alert");
		errAlert.text(msg);
		errAlert.show();
	}
});